/*
 * SelectPLIDialog.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.gui.dialogs;

import java.awt.event.*;
import java.util.Iterator;
import javax.swing.*;
import javax.swing.border.*;

/**
 * A dialog class for selecting a path link index (PLI) from a list of two or
 * more using radio buttons. Typically used to resolve situations where the user
 * is selecting a position along a route path (for instance, when placing a 
 * stop), and clicks on a node that is visited more than once by the route (i.e.
 * the route loops back on itself).
 * 
 * @author demory
 */
public class SelectPLIDialog extends JDialog implements ActionListener {

  private JButton okBtn_,  cancelBtn_;
  private ButtonGroup rBtnGrp_;
  private int selPLI_;

  public SelectPLIDialog(JFrame frame, Iterator<Integer> plis) {
    super(frame, "Select PLI", true);
    selPLI_ = -1;

    okBtn_ = new JButton("OK");
    okBtn_.addActionListener(this);
    cancelBtn_ = new JButton("Cancel");
    cancelBtn_.addActionListener(this);

    JPanel btnRow = new JPanel();
    btnRow.setLayout(new BoxLayout(btnRow, BoxLayout.X_AXIS));
    btnRow.add(okBtn_);
    btnRow.add(Box.createHorizontalStrut(5));
    btnRow.add(cancelBtn_);

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
    topPanel.add(new JLabel("Please specify the desired location:"));
    rBtnGrp_ = new ButtonGroup();
    topPanel.add(Box.createVerticalStrut(4));
    while (plis.hasNext()) {
      JRadioButton rBtn = new JRadioButton("Path link index: " + plis.next());
      rBtnGrp_.add(rBtn);
      rBtn.addActionListener(this);
      topPanel.add(rBtn);
    }

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    topPanel.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(topPanel);
    mainPanel.add(Box.createVerticalStrut(6));
    btnRow.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(btnRow);

    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(mainPanel);

    this.pack();
    this.setLocation((frame.getWidth() / 2) - getWidth() / 2 + frame.getX(),
            (frame.getHeight() / 2) - getHeight() / 2 + frame.getY());
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okBtn_) {
      setVisible(false);
    } else if (e.getSource() == cancelBtn_) {
      selPLI_ = -1;
      setVisible(false);
    } else if (e.getSource() instanceof JRadioButton) {
      String text = ((JRadioButton) e.getSource()).getText();
      selPLI_ = new Integer(text.split(":")[1].trim()).intValue();
    }
  }

  public int getSelectedPLI() {
    return selPLI_;
  }
}




